package com.learn.project2.controller.wx.coupon;

import com.learn.project2.bean.MarketUser;
import com.learn.project2.model.bo.wx.coupon.CouponExchangeBO2;
import com.learn.project2.model.bo.wx.coupon.CouponReceiveBO2;
import com.learn.project2.model.vo.wx.coupon.CouponListVO2;
import com.learn.project2.model.vo.wx.coupon.CouponMylistVO2;
import com.learn.project2.model.vo.wx.coupon.CouponSelectListVO2;
import com.learn.project2.service.wx.coupon.CouponService2;
import com.learn.project2.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author 岳子昂
 * @date 2022-01-10 14:19
 * @version 1.0
 */
@RestController
@RequestMapping("wx/coupon")
public class CouponController2 {

    @Autowired
    CouponService2 couponService2;

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 14:27
     */
    @RequestMapping("list")
    public Result list(Integer page, Integer limit){
        CouponListVO2 couponListVO = couponService2.list(page,limit);

        return Result.ok(couponListVO);
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 14:27
     */
    @RequestMapping("mylist")
    public Result myList(Short status, Integer page, Integer limit){
        // todo:要根据 userId 来确定目前的user是哪一个，从而获取其对应的优惠券表
        Object principal = SecurityUtils.getSubject().getPrincipal();
        MarketUser marketUser = (MarketUser) principal;
        Integer userId = marketUser.getId();

        CouponMylistVO2 couponMylistVO2 = couponService2.myList(status,page,limit,userId);

        return Result.ok(couponMylistVO2);
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 14:27
     */
    @RequestMapping("selectlist")
    public Result selectList(Integer cartId,Integer grouponRulesId){
        // todo:要根据 userId 来确定目前的user是哪一个，从而获取其对应的优惠券表
        Object principal = SecurityUtils.getSubject().getPrincipal();
        MarketUser marketUser = (MarketUser) principal;
        Integer userId = marketUser.getId();

        // 离谱的接口，疑惑的脑袋
        CouponSelectListVO2 couponSelectListVO2 = couponService2.selectList(userId, cartId);



        return Result.ok(couponSelectListVO2);
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 14:27
     */
    @RequestMapping("receive")
    public Result receive(@RequestBody CouponReceiveBO2 couponReceiveBO2 ){
        // todo:要根据 userId 来确定目前的user是哪一个，从而获取其对应的优惠券表
        //未登录返回登陆界面（return 501）
        if (SecurityUtils.getSubject().getPrincipal() == null){
            return Result.fail2("请登录");
        }
        Object principal = SecurityUtils.getSubject().getPrincipal();
        MarketUser marketUser = (MarketUser) principal;
        Integer userId = marketUser.getId();
        int res = couponService2.receive(couponReceiveBO2.getCouponId(),userId);
        // 0 - 领取成功
        // 1 - 领取到达最大值
        // 2 - 已被领取完
        if (res == 0){
            return Result.ok("领取成功");
        }else if (res == 1){
            return Result.fail(740,"已领取最大数量");
        }else if (res == 2){
            return Result.fail(740,"已被领取完");
        }else {
            return null;
        }
    }

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-10 14:27
     */
    @RequestMapping("exchange")
    public Result exchange(@RequestBody CouponExchangeBO2 couponExchangeBO2 ){
        // todo:要根据 userId 来确定目前的user是哪一个，从而获取其对应的优惠券表
        Object principal = SecurityUtils.getSubject().getPrincipal();
        MarketUser marketUser = (MarketUser) principal;
        Integer userId = marketUser.getId();
        String code = couponExchangeBO2.getCode();
        int res = couponService2.exchange(code, userId);
        // 0 - 领取成功
        // 1 - 兑换码不正确
        if (res == 0){
            return Result.ok("领取成功");
        }else if (res == 1){
            return Result.fail(740,"您已到达优惠券领取数量最大值");
        }else if (res == 2){
            return Result.fail(740,"很抱歉，优惠券已被领取完");
        }else if (res == 3){
            return Result.fail(742,"很抱歉，兑换码不正确");
        }else {
            return null;
        }
    }
}
